<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true">
            <el-form-item label="小区名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入小区名称" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="联系电话" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入联系电话" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="属性" prop="attrIds">
                <el-select v-model="queryParams.attrIds" placeholder="请选择属性" clearable size="small" multiple filterable>
                    <el-option v-for="(item, index) in attrList" :key="index" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="地标" prop="landmarkIds">
                <el-select v-model="queryParams.landmarkIds" style="width:300px" placeholder="请选择地标" clearable
                    size="small" multiple filterable collapse-tags>
                    <el-option v-for="(item, index) in landmarkList" :key="index"
                        :label="item.name + '---' + item.address" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
                    <el-option v-for="(item, index) in statusList" :key="index" :label="item.label"
                        :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['mall:quarter:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['mall:quarter:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="info" icon="el-icon-upload2" size="mini" @click="handleImport"
                    v-hasPermi="['mall:quarter:import']">导入</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['mall:quarter:export']">导出</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="quarterList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="小区名称" align="center" prop="name" />
            <el-table-column label="详细地址" align="center" prop="address" min-width="120" show-overflow-tooltip />
            <el-table-column label="联系电话" align="center" prop="phone" />
            <el-table-column label="属性" align="center" prop="attrNames" min-width="120" show-overflow-tooltip />
            <el-table-column label="地标" align="center" prop="landmarkNames" min-width="120" show-overflow-tooltip />
            <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    <div v-for="(item, index) in statusList" :key="index">
                        <el-tag v-if="item.value == scope.row.status" :type="item.value == '1' ? 'success' : 'danger'">
                            {{ item.label }}
                        </el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-info" @click="handleInfo(scope.row)">详情</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['mall:quarter:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['mall:quarter:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 抽屉页 -->
        <drawer ref="dDom" @init="getList" :attrList="attrList" :landmarkList="landmarkList" :statusList="statusList" />

        <!-- 详情 -->
        <detailsDrawer ref="detailsDom" :attrList="attrList" :landmarkList="landmarkList" :statusList="statusList" />

        <!-- 地标导入对话框 -->
        <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
            <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">
                    将文件拖到此处，或
                    <em>点击上传</em>
                </div>
                <div class="el-upload__tip" slot="tip">
                    <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
                </div>
                <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listQuarter, delQuarter, exportQuarter, importTemplate } from "@/api/mall/quarter";
import { getToken } from "@/utils/auth";
import { listGoodsAttr } from "@/api/mall/goodsAttr";
import { listLandmark } from "@/api/mall/landmark";
import drawer from "./drawer.vue"
import detailsDrawer from "./detailsDrawer.vue";
export default {
    name: "Quarter",
    components: { drawer, detailsDrawer },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 租房-小区信息表格数据
            quarterList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                deptId: null,
                name: null,
                address: null,
                lat: null,
                lng: null,
                phone: null,
                attrIds: null,
                landmarkIds: null,
                imgs: null,
                status: null,
                introduce: null,
            },
            statusList: [
                { label: '下架', value: '0' },
                { label: '上架', value: '1' },
            ],
            // 属性列表
            attrList: [],
            // 地标列表
            landmarkList: [],
            // 导入参数
            upload: {
                // 是否显示弹出层
                open: false,
                // 弹出层标题
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getToken() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + "/mall/quarter/importData"
            },
            attrIds: [],
            landmarkIds: [],
        };
    },
    created() {
        this.getList();
        this.getAttrList();
        this.getLandmarkList();
    },
    methods: {
        /** 查询租房-小区信息列表 */
        getList() {
            this.loading = true;
            listQuarter(this.queryParams).then(response => {
                this.quarterList = response.rows;
                this.total = response.total;
                this.queryParams.attrIds = this.attrIds;
                this.queryParams.landmarkIds = this.landmarkIds;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getJoin();
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },

        /**
       * 详情
       * @param {*} row 
       */
        handleInfo(row) {
            this.$refs.detailsDom.show(row)
        },
        /**
        * 切割数据
        */
        getJoin() {
            this.attrIds = this.queryParams.attrIds;
            this.landmarkIds = this.queryParams.landmarkIds;
            this.queryParams.attrIds = this.queryParams.attrIds.join(",");
            this.queryParams.landmarkIds = this.queryParams.landmarkIds.join(",");
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.$refs.dDom.iShow();
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.$refs.dDom.iShow(row);
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除租房-小区信息编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delQuarter(ids);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(function () { });
        },
        /** 导出按钮操作 */
        handleExport() {
            // const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有租房-小区信息数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return exportQuarter();
            }).then(response => {
                this.download(response.msg);
            }).catch(function () { });
        },
        /**
         * 属性列表
         */
        getAttrList() {
            listGoodsAttr().then(response => {
                this.attrList = response.rows;
            });
        },
        /**
         * 地标列表
         */
        getLandmarkList() {
            listLandmark().then(response => {
                this.landmarkList = response.rows;
            })
        },
        /** 导入按钮操作 */
        handleImport() {
            this.upload.title = "小区导入";
            this.upload.open = true;
        },
        /** 下载模板操作 */
        importTemplate() {
            importTemplate().then(response => {
                this.download(response.msg);
            });
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
            this.getList();
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        }
    }
};
</script>